<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/common/commonCss.jsp"/>
<jsp:include page="/common/commonJs.jsp"/>
<script src="${basePath}commonJs/jquery.ztree.all.js"></script>
<script language="javascript" src="${basePath }commonJs/verify.js"></script>
<title>嘉童游乐中心-商品信息</title>
</head>
	<script type="text/javascript">
		 //标记被选中节点
		var selectNode=null;
		var treeNodes=[];
		var updateFlag;
		var deleteFlag;
		var  usedFlag;//启用
		var  unusedFlag;//停用
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			callback:{
				onClick:zTreeonclick
			}
		};
		$(document).ready(function(){
			//验证当前用户是否有修改的权限按钮
			updateFlag=isVerify("${basePath}", "product:update");
			//验证当前用户是否有删除（批量删除的权限）
			deleteFlag=isVerify("${basePath}", "product:delete");
			//启用
			usedFlag=isVerify("${basePath}", "product:used");
			//停用
			unusedFlag=isVerify("${basePath}", "product:unused");
			initParams();
			getTreeData();
			$.fn.zTree.init($("#sptree"), setting, treeNodes);
			setDefaultForTree();
		});
		function zTreeonclick(event, treeId, treeNode) {
			var tId=treeNode.tId;
			if(tId!="sptree_1"){
				$("#sptree_1_a").removeClass("curSelectedNode");
			}
			selectNode = treeNode;// 每次点击赋值 
			$("#resverseType").val(selectNode.resverseType);
			var  fkPrdcateId=treeNode.id;
			$("#fkPrdcateId").val(fkPrdcateId);
			$("#pageNo").val("1");
			initParams();
		};
		
/**
 * 编辑按钮事件
 */
function editTree() {
	if (selectNode == null) {
		alertWarning("请先选择要编辑的 商品分类");
		return;
	} 
	$('#updateCate').window('open');
	$("#updateCateName").val(selectNode.name);
	$("#pkPrdcateId").val(selectNode.id);
}
//修改商品分类
function updateProductCate(){
	var url="${basePath}scmInvocing/productInfo/updateProductCate.shtml";
	var cateName=$("#updateCateName").val();
	if(cateName==""||$.trim(cateName)==""){
		alertWarning($common_msg_empty);
		return ;
	}
	$.ajax({
		url:url,
		data:$("#updateCateForm").serialize(),
		dataType:"text",
		type:"post",
		async:false,
		error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
		},
		success:function(result){
			if(result=="success"){
				$("#updateCate").window('close');
				alertInfo($common_edit_success);
				treeNodes=[];
				getTreeData();
				$.fn.zTree.init($("#sptree"), setting, treeNodes);
				setDefaultForTree();
			}			
		}
		
	});
}
//设置ztrre默认选中,以及样式
function setDefaultForTree(){
	$("#sptree_1_a").addClass("curSelectedNode");
	selectNode=treeNodes[0];
	$("#resverseType").val(selectNode.resverseType);
}
//增加树
function addTree(){
	if(selectNode==null){
		alertWarning("请先选择一个商品分类");
		return;
	}
	$('#addCate').window('open');
	$("#addCateName").val("");
	$("#fkParPrdcateId").val(selectNode.id);
	
}
//根据Id查询商品,并跳转到修改商品页面
function findProductById(obj){
	var pkPrdId=$(obj).attr("pkPrdId");
	var cateId1=$(obj).attr("cateId1");
	var cateId2=$(obj).attr("cateId2");
	var resverseType=$(obj).attr("resverseType");
	window.location.href="${basePath}scmInvocing/productInfo/forwardToUpdatePage.shtml?pkPrdId="+pkPrdId+"&resverseType="+resverseType+"&cateId1="+cateId1+"&cateId2="+cateId2+"";
}
//初始化参数
function initParams() {
	setRadioVal();
	$("#pageNo").val("1");
	var myform = $("#productInfo");
	var pagebean = new PageBean(myform);
	pagebean.setTableId("showData");
	pagebean.setFootId("foot");
	pagebean.setCallback(showData);
	setPagebeanObj(pagebean); //全局化pageBean对象
	pagebean.doPage();  //执行分页
}
//勾选单选按钮时是否全选
function isSelected(selectName,selectAllId,tableId){
	var length=$("input:[name='"+selectName+"']:checked").length;
	var pageSize=$("#"+tableId).find("tr").length-1;
	if(pageSize==length){//全选
		$("#"+selectAllId).attr("checked",true);
	}else{
		$("#"+selectAllId).attr("checked",false);
	} 
}
//展示数据
function showData(result){
	trimJsonArray(result);
	if(deleteFlag==false&&usedFlag==false&&unusedFlag==false){
		$("#deleteColumn").remove();
	}
	$(result).each(function(index,item){
		var html="<tr>";
		if(deleteFlag==false&&usedFlag==false&&unusedFlag==false){
			 html+="";
		}else{
			 html+="<td><input type='checkbox'    onclick='isSelected(\"isSelectAll\",\"selectAll\",\"showData\");'  name='isSelectAll'  value="+item.pkPrdId+" class='idFlag' ></td>";
		}
		 html+="<td>"+(index+1)+"</td>";
		// html+="<td class='lv'><a href='#' onclick='findProductById(this);' pkPrdId="+item.pkPrdId+"  resverseType="+item.resverseType+"  cateId1='1'>"+item.pkPrdId+"</a></td>";
		  if(updateFlag==true){
			 html+="<td class='lv'><a href='#' onclick='findProductById(this);' cateId2='"+item.unitName+"'  pkPrdId='"+item.pkPrdId+"'  resverseType='"+item.resverseType+"'  cateId1='1'>"+item.pkPrdId+"</a></td>";
	     }else{
			 html+="<td class='lv'><a href='#' onclick='findProductById(this);' cateId2='"+item.unitName+"'   pkPrdId='"+item.pkPrdId+"'  resverseType='"+item.resverseType+"'  cateId1='2'>"+item.pkPrdId+"</a></td>";
	     } 
		 if(item.prdName.length>8){
			 html+="<td title="+item.prdName+">"+item.prdName.substring(0,8)+"...</td>";
		 }else{
			 html+="<td>"+item.prdName+"</td>";
		 }
		 html+="<td>"+item.cateName+"</td>";
		 if(item.prdSpec==""||item.prdSpec==null){
			 html+="<td>无</td>";
		 }else{
			 html+="<td>"+item.prdSpec+"</td>";
		 }
		 if(item.unitName==""||item.unitName==null){
			 html+="<td>无</td>";
			 
		 }else{
			 html+="<td>"+item.unitName+"</td>";
		 }
		 if(item.comments==null||item.comments==""){
			 html+="<td>无</td>";
		 }else{
			 if(item.comments.length>5){
					html+="<td title="+item.comments+">"+item.comments.substring(0,5)+"...</td>";
			 }else{
					html+="<td>"+item.comments+"</td>";
			 }
		 }
		 if(item.prdStatus=='1'){
			 html+="<td>正常</td>";			 
		 }else if(item.prdStatus=='2'){
			 html+="<td>停用</td>";
		 }else{
			 html+="<td>未定价</td>";
		 }
		 if(deleteFlag==true){
			 html+="<td><input type='button' class='btn_l'  value='删除'  onclick='deleteProduct(this);' pkPrdId="+item.pkPrdId+" isBatch='false' /></td></tr>";
		 }else{
			 html+="<td></td></tr>";
		 }
		$("#showData").find("tr:eq("+index+")").after(html);
	});
}
		/*得到商品分类的数据信息*/
		function   getTreeData(){
					$.ajax({
							url:"${basePath}scmInvocing/productInfo/findCategoryList.shtml",
							dataType:"json",
							type:"post",
		      	 			async:false,
		      	 			error : function(XMLHttpRequest) {
		      	 				ajaxError(XMLHttpRequest);
		      	 			},
		      	 			success:function(result){
		      	 				var node;
		      	 				$(result).each(function(id,item){
		      	 					if(id==0){
			      	 					node={id:item.pkPrdcateId,pId:item.fkParPrdcateId,name:item.cateName,open:true,reserveFlag:item.reserveFlag,resverseType:item.resverseType};
		      	 					}else{
			      	 					node={id:item.pkPrdcateId,pId:item.fkParPrdcateId,name:item.cateName,reserveFlag:item.reserveFlag,resverseType:item.resverseType};
		      	 					}
		      	 					treeNodes.push(node);
		      	 				});
		      	 			}
						
					});
		}
		/*删除商品信息*/
		function deleteProduct(obj){
				
				var isBatch=$(obj).attr("isBatch");
				var pkPrdId="";
				var idList=[];
				var idFlagObj=$(".idFlag:checked");
				if(isBatch=="false"){
					pkPrdId=$(obj).attr("pkPrdId");
				}else{
					if($(idFlagObj).length<1){
						alertWarning("请选择要删除的商品");
						return;
					}
					$(idFlagObj).each(function(){
						idList.push($(this).val());
					});
					idList=idList.join();
				}
				$.ajax({
					url:"${basePath}scmInvocing/productInfo/canAbled.shtml",
					data:{"idList":idList,"pkPrdId":pkPrdId},
					dataType:"text",
					type:"post",
					async:false,
					error : function(XMLHttpRequest) {
      	 				ajaxError(XMLHttpRequest);
      	 			},
					success:function(result){
						if(result=="0"||result=="2"){
							var message="";
							if(result=="0"){
								message="您确定要执行删除操作吗?";
							}else{
								if(isBatch=="false"){
									message="该商品正在使用中,您确定要删除吗?";
								}else{
									message="选择的商品中有正在使用的商品，您确定要删除吗?";
								}
							}
							$.messager.confirm('警告', message, function(isOk) {
								if (isOk) {
										$.ajax({
											url:"${basePath}scmInvocing/productInfo/deleteProduct.shtml",
											data:{"idList":idList,"pkPrdId":pkPrdId},
											dataType:"text",
											type:"post",
											async:false,
											error : function(XMLHttpRequest) {
						      	 				ajaxError(XMLHttpRequest);
						      	 			},
											success:function(result){
												//去掉全选的勾选状态
												$("#selectAll").attr("checked",false);
												initParams();
												alertInfo($common_delete_success);
											},
											error:function(){
												alertWarning($common_delete_failure);
											}
										});
								}
							});
						}else{
							if(isBatch=="false"){
								alertWarning("该商品正在使用中不允许删除!");
								return ;
							}else{
								alertWarning("选择的商品中包含不允许删除的商品，请重新选择!");
								return;
							}
						}
					}
				});
		}
		/*修改商品的状态*/
		function updateStatus(obj){
			var prdStatus=$(obj).attr("prdStatus");
			var idList=[];
			var idFlagObj=$(".idFlag:checked");
			if($(idFlagObj).length<1){
				if(prdStatus=="2"){
					alertWarning("请选择停用的商品!");		
				}else{
						alertWarning("请选择启用的商品!");		
				}
				return ;
			}
			$(idFlagObj).each(function(){
				idList.push($(this).val());
			});
			//停用
			if(prdStatus=="2"){
				$.ajax({
					url:"${basePath}scmInvocing/productInfo/canAbled.shtml",
					data:{"idList":idList.join()},
					dataType:"json",
					type:"post",
					async:false,
					error : function(XMLHttpRequest) {
      	 				ajaxError(XMLHttpRequest);
      	 			},
					success:function(result){
						/* var length=$(idFlagObj).length;
						if(length==){} */
						if(result=="0"||result=="2"){
							var message="";
							if(result=="0"){
								  	message="您确定要执行停用操作吗?";
							}else{
									message="您选择的商品中有正在使用的商品,确定要停用吗?"; 
							}
							$.messager.confirm('警告', message, function(isOk) {
								if (isOk) {
									$.ajax({
										url:"${basePath}scmInvocing/productInfo/updateProduct.shtml",
										data:{"idList":idList.join(),"prdStatus":prdStatus},
										dataType:"text",
										type:"post",
										async:false,
										error : function(XMLHttpRequest) {
						  	 				ajaxError(XMLHttpRequest);
						  	 			},
										success:function(result){
											$("#selectAll").attr("checked",false);
											initParams();
											if(prdStatus=="2"){
												alertInfo("停用成功!");		
											}else{
												alertInfo("启用成功!");		
											}
										}
									});
								}
							});
						}else{
								alertWarning("您选择的商品中包含不允许停用的商品，请重新选择!");
								return;
						}
					}
				});
			}else{
				$.ajax({
					url:"${basePath}scmInvocing/productInfo/updateProduct.shtml",
					data:{"idList":idList.join(),"prdStatus":prdStatus},
					dataType:"text",
					type:"post",
					async:false,
					error : function(XMLHttpRequest) {
	  	 				ajaxError(XMLHttpRequest);
	  	 			},
					success:function(result){
						$("#selectAll").attr("checked",false);
						initParams();
						if(prdStatus=="2"){
							alertInfo("停用成功!");		
						}else{
							alertInfo("启用成功!");		
						}
					}
				});
			}		
		}
		function forwordAddProduct(){ 
			//判断是否选择具体的商品分类
			var id=selectNode.id;
			if(id=="001"){
				alertWarning("请选择一个具体商品分类");
				return;
			}
			var  pageDiv=document.getElementById("foot"); 
			var temp=document.createElement("form"); 
			temp.action="${basePath}scmInvocing/productInfo/forwordAddProduct.shtml";
			temp.method="post"; 
			temp.style.visibility="hidden"; 
			temp.name="loadConfigPage"; 
			var restype=document.createElement("input"); 
			restype.name="resverseType"; 
			restype.value=$("#resverseType").val();
			var  cateNames=document.createElement("input"); 
			cateNames.name="cateName"; 
			cateNames.value=selectNode.name;
			
			var  prdcateId=document.createElement("input"); 
			prdcateId.name="fkPrdcateId"; 
			prdcateId.value=selectNode.id;
			
			temp.appendChild(restype); 
			temp.appendChild(cateNames); 
			temp.appendChild(prdcateId); 
			pageDiv.appendChild(temp); 
			temp.submit(); 
			} 
		//查询商品信息
		function findProductList(){
				initParams();
		}
		//将选中的radio赋值
		function setRadioVal(){
			var selVal=$("#tPrdStatus option:selected").val();
			$("#prdStatus").val(selVal);
		}
		function deleteCate(){
				var reserveFlag=selectNode.reserveFlag; //系统预设标及
				var isParent=selectNode.isParent;
				var pkPrdcateId=selectNode.id;
				if(reserveFlag=='1'){
					alertWarning($reserver_delete_system);
					return;
				}
				if(isParent==true){
					alertWarning($delete_isParent_worning);
					return;
				}
				var url="${basePath}scmInvocing/productInfo/deleteAbled.shtml";
				$.ajax({
					url:url,
					dataType:"text",
					type:"post",
					data:{"cateId":pkPrdcateId},
					error : function(XMLHttpRequest) {
      	 				ajaxError(XMLHttpRequest);
      	 			},
					success:function(result){
						if(result=="0"){
							$.messager.confirm('温馨提示', '您确定要删除该商品分类吗?', function(r) {
								if (r) {
									$.ajax({
										url:"${basePath}scmInvocing/productInfo/deleteProductCate.shtml",
										dataType:"text",
										type:"post",
										data:{"pkPrdcateId":pkPrdcateId},
										error : function(XMLHttpRequest) {
					      	 				ajaxError(XMLHttpRequest);
					      	 			},
										success:function(result){
											if(result=="success"){
												alertInfo($common_delete_success);
												treeNodes=[];
												getTreeData();
												$.fn.zTree.init($("#sptree"), setting, treeNodes);
												setDefaultForTree();
											}
										}
									});
								}
							});
						}else{
							alertWarning("该商品分类下有商品，不允许删除!");
							return;
						}
					}
				});
		}
		//新增商品分类
		function  addProductCate(){
				var val=$("#addCateName").val();
				if(val==""||$.trim(val)==""){
					alertWarning($common_msg_empty);
					return ;
				}
				if($("#resverseType").val()==""){
					$("#resverseType").val("6");
				}
				var url="${basePath}scmInvocing/productInfo/addProductCate.shtml";
				$.ajax({
					url:url,
					dataType:"text",
					type:"post",
					data:$("#addCateForm").serialize(),
					error : function(XMLHttpRequest) {
      	 				ajaxError(XMLHttpRequest);
      	 			},
					success:function(result){
						if(result=="success"){
							$("#addCate").window('close');
							alertInfo($common_create_success);
							treeNodes=[];
							getTreeData();
							$.fn.zTree.init($("#sptree"), setting, treeNodes);
							setDefaultForTree();
						}
					}
				});
		}
		//设置全选按钮全选
		function setSelectAll(className,thisobj){
			if($(thisobj).attr("checked")=="checked"){
					$("."+className).attr("checked",true);
			}else{
				$("."+className).attr("checked",false);
			}
		}
	</script>
    
<body class="bg">
<div class="content m_auto">
	<div class="content_l mt10">
    	<ul>
    		<shiro:hasPermission name="scmInvocing/productInfo/forwardToProductInfo.shtml">
    				  <li  class="content_hover"><a href="${basePath}scmInvocing/productInfo/forwardToProductInfo.shtml"><i class="nav_icon_b"></i><br>商品信息</a></li>
    		</shiro:hasPermission>
    		 <shiro:hasPermission name="scmInvocing/countUnit/forwardToUnitInfo.shtml">
    		 		<li><a href="${basePath}scmInvocing/countUnit/forwardToUnitInfo.shtml"><i class="nav_icon_b"></i><br>单位设置</a></li>
    		 </shiro:hasPermission>
    		 <shiro:hasPermission name="scmInvocing/supplier/forwardToSupply.shtml">
    		 		   <li><a href="${basePath}scmInvocing/supplier/forwardToSupply.shtml"><i class="nav_icon_a"></i><br>供应商信息</a></li>
    		 </shiro:hasPermission>
    		  <shiro:hasPermission name="scmInvocing/priceManager/forwardToList.shtml">
    		 		  <li><a href="${basePath}scmInvocing/priceManager/forwardToList.shtml"><i class="nav_icon_f"></i><br>价格管理</a></li>
    		 </shiro:hasPermission>
    		   <shiro:hasPermission name="scmInvocing/scmPurOrder/findInvoPurOrder.shtml">
            	  <li><a href="${basePath}scmInvocing/scmPurOrder/findInvoPurOrder.shtml"><i class="nav_icon_b"></i><br>采购订单</a></li>
    		 </shiro:hasPermission>
    		   <shiro:hasPermission name="scmInvocing/scmPayForm/findInvoOrderPay.shtml">
           		   <li><a href="${basePath}scmInvocing/scmPayForm/findInvoOrderPay.shtml"><i class="nav_icon_b"></i><br>付款申请</a></li>
    		 </shiro:hasPermission>
        </ul>
    </div>
    <div class="content_r mt10">
    	<div class="p10">
    	<div class="file_single_dl f14 fixed">
    		<form id="productInfo" action="${basePath}scmInvocing/productInfo/findProductList.shtml">
    		<input type="hidden" name="pageNo" id="pageNo" value="1">
    		<input type="hidden" name="pageSize" id="pageSize" value="8">
    	   	<input type="hidden" name="prdStatus" id="prdStatus" value=""/>
    	   	<input type="hidden" name="fkPrdcateId" id="fkPrdcateId" value=""/>
        	<dl class="fixed" style="overflow:hidden">
            	<dd><label>商品编号：</label><input type="text" class="iput_t w200" name="pkPrdId"></dd>
                <dd><label>商品名称：</label><input type="text" class="iput_t w200" name="prdName"></dd>
                <dd><label>商品状态：</label>
                    <div class="uboxstyle">
                        <select name="tPrdStatus" id="tPrdStatus">
                            <option value="">全部</option>
                            <option value="1">正常</option>
                            <option value="2">停用</option>
                        </select>
                    </div>
                </dd>
<!--                 <dd><label>商品状态：</label><input type="radio"  checked="checked" name="tPrdStatus"  value="">全部<input type="radio"  name="tPrdStatus" value="1" class="ml10">正常<input type='radio'  class="ml10" name="tPrdStatus" value="2">停用</dd>
 -->            </dl>
            </form>
            <div>
              <input type="button" class="btn fr" value="查询" onclick="findProductList();">
            </div>
        </div>
       
        <div class="tab_normal mt10">
           
            <ul class=" fixed" id="tab_a"></ul>
            <div class="payment_l tc mt10"  style="width: 300px !important;">
               <shiro:hasPermission name="category:add">
               			 <input type="button" value="增加" class="btn ml10 w70"  onclick="addTree();">
               </shiro:hasPermission>
               <shiro:hasPermission name="category:update">
               			    <input type="button" value="编辑" class="btn ml10 w70" onclick="editTree();">
               </shiro:hasPermission>
                 <shiro:hasPermission name="category:delete">
              			     <input type="button" value="删除" class="btn ml10 w70" onclick="deleteCate();">
               </shiro:hasPermission>
                 <div class="ztree m10" id="sptree" style="overflow-y: auto;overflow-x: auto;width: 270px; height: 412px;">
                 
                 
                 </div>
                
                
            </div>  
            <div style="float:left; width:837px; margin-left:10px;">
          				<div class="mt10 fl">
          						 <shiro:hasPermission name="product:add">
                   						 <input class="btn w70" type="button" onclick="forwordAddProduct();" value="新增" />
               					</shiro:hasPermission>
          						<shiro:hasPermission name="product:delete">
                   					<input class="btn" type="button" value="批量删除" onclick="deleteProduct(this);" isbatch="true"/>
               					</shiro:hasPermission>
               					<shiro:hasPermission name="product:used">
                  						  <input class="btn w70" type="button" value="启用" prdStatus="1" onclick="updateStatus(this);"/>
               					</shiro:hasPermission>
               					<shiro:hasPermission name="product:unused">
               					          <input class="btn w70" type="button" value="停用"  prdStatus="2" onclick="updateStatus(this);"/>
               					</shiro:hasPermission>
               					
                		</div>
          				 <div class="clear"></div>
          				 <table class="table mt10" id="showData">
                    	<tr>
                       		 <th id="deleteColumn"><input type="checkbox" id="selectAll" onclick="setSelectAll('idFlag',this);"/></th>
                        	<th>序号</th>
                            <th>商品编号</th>
                            <th>商品名称</th>
                            <th>商品分类</th>
                            <th>规格</th>
                            <th>计量单位</th>
                            <th>备注</th>
                            <th>状态</th> 
                            <th>操作</th>
                        </tr>
                    </table>
                    <!-- 翻页 -->
               	 <div class="page fr m10" id="foot">
                        
                  </div>
          		
            </div>
          	
                </div>
        </div>
    </div>
</div>

<!---弹出层 成员信息编w辑--->
<div id="addCate" class="easyui-window" title="新增分类" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:400px; height:150px;position:relative;">
	<form id="addCateForm">
	<input type="hidden" name="fkParPrdcateId" id="fkParPrdcateId" value=""/>
	<input type="hidden" name="resverseType" id="resverseType" value=""/>
	<div class="editor_h1 fixed">
      <div class="editor fl">
            <ul>
                <li><label>分类名称：</label><input type="text" class="iput_t w160" name="cateName" id="addCateName"></li>
            </ul>
        </div>
    </div>
    </form>
    <div class="btm_btn btm_bg">
    	<dl>
        	<dd><input type="button" class="btn" onclick="addProductCate();" value="确定"></dd>
            <dd><input type="button" class="btn" onclick="$('#addCate').window('close')" value="取消"></dd>
        </dl>
    </div>
</div>

<div id="updateCate" class="easyui-window" title="编辑分类" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:400px; height:150px;position:relative;">
	<form id="updateCateForm">
	<div class="editor_h1 fixed">
	<input type="hidden" id="pkPrdcateId" name="pkPrdcateId" value=""/>
      <div class="editor fl">
            <ul>
                <li><label>分类名称：</label><input name="cateName" id="updateCateName" type="text" class="iput_t w160"></li>
            </ul>
        </div>
    </div>
    </form>
    <div class="btm_btn btm_bg">
    	<dl>
        	<dd><input type="button" class="btn" onclick="updateProductCate();" value="确定"></dd>
            <dd><input type="button" class="btn" onclick="$('#updateCate').window('close')" value="取消"></dd>
        </dl>
    </div>
</div>
</body>
</html>
